package com.ruoyi.backend.controller;

import com.ruoyi.backend.dto.RestorationDataUploadDTO;
import com.ruoyi.backend.pojo.RestorationData;
import com.ruoyi.backend.service.RestorationDataService;
import com.ruoyi.backend.vo.RestorationDataVO;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.DataSourceType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

@Api(tags = "生态修复工程数据管理接口", value = "(MySQL主导执行)所有接口已完成测试")
@RequestMapping("/backend/restorationData")
@RestController
public class RestorationDataController {

    @Autowired
    private RestorationDataService restorationDataService;

    @DataSource(DataSourceType.MASTER)
    @ApiOperation(value = "上传生态修复工程数据接口")
    @PostMapping("/upload")
    public R<String> upload(@ModelAttribute @Validated(RestorationDataUploadDTO.Upload.class)
                                RestorationDataUploadDTO restorationDataUploadDTO) throws IOException {

        restorationDataService.upload(restorationDataUploadDTO);
        return R.ok("生态修复工程数据上传成功");
    }

    @DataSource(DataSourceType.MASTER)
    @ApiOperation(value = "删除生态修复工程数据接口")
    @DeleteMapping("/delete")
    public R<String> delete(@RequestParam("id") Integer id){

        restorationDataService.delete(id);
        return R.ok("生态修复工程数据删除成功");
    }

    @DataSource(DataSourceType.MASTER)
    @ApiOperation(value = "更新生态修复工程数据接口")
    @PutMapping("/update")
    public R<String> update(@RequestBody @Validated(RestorationData.Update.class) RestorationData restorationData){

        restorationDataService.update(restorationData);
        return R.ok("生态修复工程数据更新成功");
    }

    @DataSource(DataSourceType.MASTER)
    @ApiOperation(value = "查询生态修复工程数据接口")
    @GetMapping("/search")
    public R<List<RestorationDataVO>> search(@RequestParam(name = "restorationId", required = false) Integer restorationId,
                                             @RequestParam(name = "reportType", required = false) String reportType,
                                             @RequestParam(name = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startTime,
                                             @RequestParam(name = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endTime,
                                             @RequestParam(name = "fileType", required = false) String fileType
                         ){

        List<RestorationDataVO> restorationDataList = restorationDataService.search(restorationId, reportType, startTime, endTime, fileType);
        return R.ok(restorationDataList, "生态修复工程数据查询成功");
    }

    @DataSource(DataSourceType.MASTER)
    @ApiOperation(value = "查询所有生态修复工程数据接口")
    @GetMapping("/all")
    public R<List<RestorationDataVO>> all(){

        List<RestorationDataVO> restorationDataList = restorationDataService.searchAll();
        return R.ok(restorationDataList, "生态修复工程数据查询成功");
    }

}
